package com.jabo.douban.demo.test;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jabo.douban.demo.widget.RoundImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * @anthor JZheng3
 * @Time: 2020/3/6
 */
public abstract class NineGridPictureLayout extends ViewGroup {
    private int mPerLineNum = 3;
    private int mSelfAvailableWidth;
    private int mSelfHeight;
    private int mChildViewWidth;
    private int mRawSpace = 6;
    private int mLineSpace = 6;
    private boolean mInit;
    private List<String> mDataInfos;
    private int mMaxNum = 9;
    private boolean mOnlyOneViewSizeFixed;

    public NineGridPictureLayout(Context context) {
        this(context, null);
    }

    public NineGridPictureLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NineGridPictureLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mInit = true;
        mDataInfos = new ArrayList<>();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        mSelfAvailableWidth = r - l - getPaddingLeft() - getPaddingRight();
        mChildViewWidth = (mSelfAvailableWidth / mPerLineNum) - ((mPerLineNum - 1) * mRawSpace);
        if (mInit) {
            mInit = false;
            notifyViewLayout();
        }
    }

    private void notifyViewLayout() {
        if (mDataInfos.isEmpty()) {
            setVisibility(GONE);
            return;
        } else {
            setVisibility(VISIBLE);
        }
        removeAllViews();
        int size = getDataSize();

        if (size == 1 && !mOnlyOneViewSizeFixed) {
            final ImageView childView = createChildView();
            childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
            final LayoutParams layoutParams = getLayoutParams();
            layoutParams.height = mChildViewWidth + getPaddingTop() + getPaddingBottom();
            setLayoutParams(layoutParams);

            loadContentOneView(childView, mDataInfos.get(0), 0, mSelfAvailableWidth);
            addView(childView);
        } else {
            int lineHeight = 0;
            int lineWidth = 0;

            layoutParams();

            for (int i = 0; i < (size > mMaxNum ? mMaxNum : size); i++) {
                ImageView child = createChildView();
                loadContentMoreViews(child, mDataInfos.get(0), i);
                if (size == 4) {//四张图需要显示两行
                    if (i == 2) {
                        lineHeight += mChildViewWidth + mLineSpace;
                        lineWidth = getPaddingLeft();
                    }
                } else {
                    if (i % mPerLineNum == 0 && i != 0) {
                        lineHeight += mChildViewWidth + mLineSpace;
                        lineWidth = getPaddingLeft();
                    }
                }
                child.layout(lineWidth, lineHeight, lineWidth + mChildViewWidth, lineHeight + mChildViewWidth);
                lineWidth += mChildViewWidth + mRawSpace;
                addView(child);
            }
        }


    }


    protected void setOneViewParam(ImageView childView, int height, int width) {
        LayoutParams layoutParamsChild = childView.getLayoutParams();
        layoutParamsChild.height = height;
        layoutParamsChild.width = width;
        childView.setLayoutParams(layoutParamsChild);
        childView.layout(0, 0, width, height);

        LayoutParams layoutParams = getLayoutParams();
        layoutParams.height = height + getPaddingTop() + getPaddingBottom();
        setLayoutParams(layoutParams);
    }

    private void layoutParams() {
        int lines = (int) Math.ceil((float) mDataInfos.size() / mPerLineNum);
        mSelfHeight = (lines - 1) * mLineSpace + lines * mChildViewWidth + getPaddingBottom() + getPaddingTop();
        LayoutParams layoutParams = getLayoutParams();
        layoutParams.height = mSelfHeight;
        setLayoutParams(layoutParams);
    }

    private ImageView createChildView() {
        ImageView imageView = new RoundImageView(getContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        LayoutParams layoutParams = new LayoutParams(mChildViewWidth, mChildViewWidth);
        imageView.setLayoutParams(layoutParams);

        return imageView;
    }

    protected int getDataSize() {
        return mDataInfos.size();
    }

    public void setDataInfos(List<String> dataInfos) {
        mDataInfos = dataInfos;
        if (!mInit) {
            notifyViewLayout();
        }
    }


    public abstract void loadContentOneView(ImageView childView, String dataInfo, int position, int parentSize);

    protected abstract void loadContentMoreViews(ImageView child, String dataInfo, int i);
}
